use crate::component::request::{Method, Request, RequestBody};
use crate::component::response::Response;
use crate::component::{ParseResult, Task};
use async_trait::async_trait;

use log::{debug, error};
use url::Url;

#[async_trait]
pub trait Spider: 'static + Send + Sync {
    type M: Send + Sync;
    type ItemType: Send + Sync;

    fn start_urls(&self) -> &[&'static str];
    fn start_tasks(&self) -> Vec<Task<Self::M, Self::ItemType>> {
        let mut tasks = vec![];
        for url in self.start_urls() {
            match Url::parse(url) {
                Ok(url) => {
                    debug!("为 [{}] 创建请求", url);
                    let req = Request::new(url, Method::GET, None, None, RequestBody::Empty);
                    let task = Task {
                        req,
                        meta: None,
                        parser: None,
                    };
                    tasks.push(task);
                }
                Err(e) => error!("创建请求失败, {}", e),
            }
        }
        tasks
    }

    async fn parse(
        &self,
        resp: Response,
        meta: Option<Self::M>,
    ) -> ParseResult<Self::M, Self::ItemType> {
        let tasks = vec![];
        let items = vec![];

        let _ = meta;
        debug!("Default parsing {}", resp.resp.text().await.unwrap());

        ParseResult::new(tasks, items)
    }
}
